import { addLink } from '@/api/admin/systemConfig/networkTopoConfig';

export default function (G6: any) {
	// Register a custom behavior: click two end nodes to add an edge
	G6.registerBehavior('click-add-edge', {
		// Set the events and the corresponding responsing function for this behavior
		getEvents() {
			return {
				'node:click': 'onClick', // The event is canvas:click, the responsing function is onClick
				mousemove: 'onMousemove', // The event is mousemove, the responsing function is onMousemove
				click: 'onEdgeClick', // The event is edge:click, the responsing function is onEdgeClick
			};
		},
		// 节点单击事件
		onClick(ev: any) {
			console.log('onClick1', ev);
			const self = this as any;
			const graph = self.graph;
			const clickShape = ev.target;
			const model = ev.item.getModel();
			const point = { x: ev.x, y: ev.y };

			if (self.addingEdge && self.edge) {
				let flag = true;
				const edgeSourceNodeModel = self.edge.getSource().getModel();
				const edges = graph.getEdges();

				for (let i = 0, len = edges.length - 1; i < len; i++) {
					const sNode = edges[i].getSource();
					const tNode = edges[i].getTarget();
					if (sNode._cfg && tNode._cfg) {
						const sourceNodeModelId = sNode.getModel().id;
						const targetNodeModelId = tNode.getModel().id;
						if (edgeSourceNodeModel.id === model.id) {
							// 防止自连
							flag = false;
							break;
						}
						if (edgeSourceNodeModel.id === sourceNodeModelId && model.id === targetNodeModelId) {
							flag = false;
							break;
						}
					}
				}
				if (flag) {
					graph.updateItem(this.edge, {
						target: model.id,
					});
					const sourceId = self.edge.getSource().getModel().id;
					const targetId = model.id;
					if (sourceId === targetId) {
						// 防止自连
						return false;
					}
					addLink({
						nodeId: model.id,
						parentNodeId: this.edge.getSource().getModel().id,
						systemId: sessionStorage.getItem('networkTopoConfig.systemId'),
					});
					this.edge = null;
					this.addingEdge = false;
				}
			} else {
				self.edge = graph.addItem('edge', {
					type: 'myCubicHorizontal',
					source: model.id,
					target: point,
					data: {
						nodeType: model.nodeType,
					},
				});
				self.addingEdge = true;
				/*if (clickShape.cfg.name === 'rightCircleShape' || clickShape.cfg.name === 'leftCircleShape'){
          self.edge = graph.addItem('edge', {
            type: 'myCubicHorizontal',
            source: model.id,
            target: point,
            data: {
              nodeType: model.nodeType
            }
          });
          self.addingEdge = true;
        }*/
			}
		},
		// 鼠标移动事件
		onMousemove(ev: any) {
			const self = this as any;
			if (self.addingEdge && self.edge) {
				const point = { x: ev.x, y: ev.y };
				// 更新边的坐标为鼠标当前的坐标
				self.graph.updateItem(self.edge, {
					target: point,
				});
			}
		},
		// 点击事件，点击canvas空白区域取消当前不完整的边
		onEdgeClick(ev: any) {
			const self = this as any;
			const currentEdge = ev.item;
			if (self.addingEdge && self.edge === currentEdge) {
				self.graph.removeItem(self.edge);
				self.edge = null;
				self.addingEdge = false;
			}
		},
	});
}
